package com.qianclass.talklive.app.activity;

import android.app.Activity;
import android.app.Application;

import com.xsj.crasheye.Crasheye;

import java.util.ArrayList;
import java.util.List;

import io.agora.rtc.RtcEngine;

/**
 * Created by apple on 15/9/9.
 */
public class AgoraApplication extends Application {

    private RtcEngine rtcEngine;
    private MessageHandler messageHandler;

    @Override
    public void onCreate(){

        super.onCreate();
        Crasheye.initWithNativeHandle(this, "06798b00");
        //自定义异常捕捉
        CrashHandler.newsIntance().init(this);

        messageHandler = new MessageHandler();
    }

    /**
     * Test vendor key:  6D7A26A1D3554A54A9F43BE6797FE3E2
     * @param vendorKey
     */
    public void setRtcEngine(String vendorKey){

        if(rtcEngine==null) {
            try {
                rtcEngine = RtcEngine.create(getApplicationContext(), vendorKey, messageHandler);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public RtcEngine getRtcEngine(){

        return rtcEngine;
    }

    public void setEngineEventHandlerActivity(BaseEngineEventHandlerActivity engineEventHandlerActivity){
        messageHandler.setActivity(engineEventHandlerActivity);
    }

    public static List<Object> activitys = new ArrayList<Object>();
    private static AgoraApplication instance;
    //获取单例模式中唯一的MyApplication实例
    public static AgoraApplication getInstance() {
        if (instance == null)
            instance = new AgoraApplication();
        return instance;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (!activitys.contains(activity))
            activitys.add(activity);
    }

    // 遍历所有Activity并finish
    public void destroy() {

        for (Object activity : activitys) {
            ((Activity) activity).finish();
        }
        System.exit(0);
    }

}
